package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        SharedPreferences sp = getSharedPreferences("todolist", Context.MODE_PRIVATE); // 获取SharedPreferences对象
        SharedPreferences.Editor editor = sp.edit();    // 获取编辑对象

        // 初始化sqlite对象
        SQLiteDatabase db = getApplicationContext().openOrCreateDatabase("testdb", Context.MODE_PRIVATE, null);
        String sql = "create table if not exists todolist(id int, text varchar(20))";  // 创建表
        db.execSQL(sql);            // 执行sql

        // 1、定义数据
        List<String> data = new ArrayList<>();
        // 读数据库
        Cursor cursor = db.rawQuery("select * from todolist", null);
        while(cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String text = cursor.getString(1);
            data.add(text);
        }

//        for(int i=0; i<50; i++) {
//            data.add("this is item " + i);
//        }
        // 2、创建Adatper
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                R.layout.listview_item,
                R.id.text,
                data
        );
        // 3、关联 adapter与listview
        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(adapter);

        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 1:获取输入框内容
                EditText editText = findViewById(R.id.et);
                String s = editText.getText().toString();
                // 2: 添加到数组
                data.add(s);
                // 通知adapter更新数据
                adapter.notifyDataSetChanged();


                // 插入数据到数据库中
                String sql = "insert into todolist(id, text) values(?, ?)";
                db.execSQL(sql, new Object[]{data.size(), editText.getText().toString()});

                // 清除输入框内容
                editText.setText("");
            }
        });

        EditText editText = findViewById(R.id.et);
        // 读取SharedPreferences，如果有内容，就显示到输入框中
        String s = sp.getString("text", "");
        editText.setText(s);    // 设置到输入框


        // 添加输入框内容变化监听事件
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String content = charSequence.toString();  // 当前输入框的内容
                editor.putString("text", content);
                editor.commit();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }
}